/***************************************************************
*  Copyright (c) 2007 by GroupMe! Team (www.groupme.net)
*  All rights reserved
*
*  This file is part of the GroupMe! Project. You can utilize
*  software of the GroupMe! project under the terms of the 
*  GNU Lesser General Public License as published by the
*  Free Software Foundation in version 2.1.
* 
*  The GNU Lesser General Public License can be found at:
*  http://www.gnu.org/licenses/lgpl.html
*
*  This copyright notice MUST APPEAR in all copies of the file!
***************************************************************/
package net.groupme.export.rss;

import de.nava.informa.core.ChannelIF;
import de.nava.informa.core.ItemIF;
import de.nava.informa.core.WithCategoriesMIF;
import de.nava.informa.impl.basic.ChannelBuilder;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.sql.DataSource;
import net.groupme.export.*;
import net.groupme.export.constants.General;
import net.groupme.export.constants.GroupConstants;
import net.groupme.export.constants.ResourceConstants;
import net.groupme.export.rdf.IRdfDataExport;
import net.groupme.model.Group;
import net.groupme.model.Resource;
import net.groupme.model.Tag;

/**
 * This class provides..
 *
 * @author Daniel Plappert
 * @version 0.1
 */
public class GroupExport implements IRssDataExport, IGroupExport {
    

    private Collection<Group> data = null;
    private ChannelBuilder builder = null;
    private boolean exportGroupAsChannel = false;
    
    
    /**
     * Creates a new instance of GroupExport
     */
    private GroupExport() {}
    
    
    /**
     * 
     * @param DataSource 
     */
    public GroupExport(ChannelBuilder builder) {
        this.builder = builder;
    }
    
    
    /**
     *
     * @param Collection<Group>
     */
    public void setData(Collection<Group> data) {
        this.data = data;
    }
    
    
    /**
     *
     * @param boolean
     */
    public void setExportGroupAsChannel(boolean value) {
        this.exportGroupAsChannel = value;
    }
    
    
    /**
     * This method creates the rss data for all groups
     * set by the setData method.
     *
     * @param ChannelIF 
     */
    public void exportData(ChannelIF channel) {
        
        if ( channel == null )
            return;

        ItemIF tempItem;        
        if ( exportGroupAsChannel ) {
        
            // get data of group
            Group tempGroup = data.iterator().next();

            // tags as category
            addTagsAsCategory(channel, tempGroup.getTags());

            // add url
            channel.setSite(tempGroup.getURL());
            
            // add description
            channel.setDescription(tempGroup.getDescription());
            
            // all resource as items...
            for(Resource tempResource : tempGroup.getResources()) {
                tempItem = builder.createItem(channel, 
                    tempResource.getName(), tempResource.getDescription(), tempResource.getURL());
                
                tempItem.setLink(tempResource.getURL());
                
                addTagsAsCategory(tempItem, tempResource.getTags());
            }            
            
        } else {
           
            // export all groups as items
            for(Group tempGroup : data) {

                tempItem = builder.createItem(channel, 
                    tempGroup.getName(), tempGroup.getDescription(), tempGroup.getURL());

                // set Link
                tempItem.setLink(tempGroup.getURL());
                
                addTagsAsCategory(tempItem, tempGroup.getTags());
            } //while 
            
        }
        
    }
    
    

    /**
     *
     *
     */
    private void addTagsAsCategory(WithCategoriesMIF object, Collection<Tag> tagCollection) {
        
        for(Tag tag : tagCollection) {
            object.addCategory(builder.createCategory(null, tag.getKeyword()));
        }        
        
    }

    
}
